package Bank;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * 管理员类
 * 实现了管理员对银行卡和用户的操作
 *@date 2014.8.17
 */
public class Admin {
	Customer[] customer = MainTest.CustomerArray;// 指向CustomerArray的引�?
	Account account[] = MainTest.AccountArray;
	private String username;
	private String password;

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
	/**
	 *  用户注册方法
	 */
	public void registerCustomer() {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the number of the customers:");
		int number=0;
		try{
		number = in.nextInt();
		}catch(InputMismatchException e){
			System.out.println("Only for input digits,please enter again");
			registerCustomer();
		}
		int customerCount = Customer.getCount();//已存在的customer的数�?
		boolean flag;
		for (int i = customerCount; i < customerCount + number; i++) {
			System.out.println("Customer" + (i+1) + ":");    //2014 8.18 +1去掉了。�?    belle&didi
			boolean flag2;
			do{
				long id;
				flag2=false;
			try{
			do {
				//System.out.println("    *****************    ");
				flag = true;
				System.out.print("Enter customer's ID:-");
				customer[i] = new Customer();

				id = in.nextLong();				
				for (int j = 0; j < i; j++) {
					if (customer[j].getCustomer_id() == id) {
						System.out.println("Sorry~ the id has exited,please enter again!");
						flag = false;
						break;                       //修改能创建相同用户id bug。�?belle&didi   20140818
					}
				}
			} while (flag == false);
			customer[i].setCustomer_id(id);
			}catch(InputMismatchException e){
				System.out.println("Only for input digits,please enter again");
				flag2=true;
			//	Scanner in1 = new Scanner(System.in);
				in.nextLine();
			}
			}while(flag2==true);
			System.out.print("Enter customer's lastname:-");
			in.nextLine();
			customer[i].setCustomer_lastname(in.nextLine());			
			System.out.print("Enter customer's name:-");
			customer[i].setCustomer_name(in.nextLine());
			System.out.print("Enter customer's username:-");
			customer[i].setCustomer_username(in.nextLine());
			boolean flag1=false;
			do{
			System.out.print("Enter customer's password(It's only for six digits):-");
			String password = "\\d{6}";
			String password1 = in.nextLine();
			if(password1.matches(password)){
			customer[i].setCustomer_password(password1);
			flag1 = true;			
			}else{
				System.out.println("The password is illegal!Please enter again~");
			}
			}while(flag1==false);
			System.out.print("Enter customer's address:-");
			customer[i].setCustomer_address(in.nextLine());
			System.out.print("Enter customer's phonenumber:-");
			
			boolean flag_phone;
		do{
			flag_phone = false;
			String phone3 = "\\d{11}";
			String phone2 = "\\d{14}";
			String phone = in.nextLine();
			if(phone.indexOf("+")==0){
			  if(phone.length()==14){
					String lphone = phone.substring(1);
					if(lphone.matches(phone2)){
						flag_phone = true;
						customer[i].setCustomer_phonenumber(phone);
					}else{
						System.out.println("It's only for digits!");
					}
				}else{
					System.out.println("There must be 14 digits after "+"+!");
					System.out.print("Please enter again:-");
				}
			}else{
				if(phone.matches(phone3)){
					flag_phone = true;
					customer[i].setCustomer_phonenumber(phone);
				}else{
					System.out.println("There must be 11 digits!");
					System.out.print("Please enter again:-");
				}
			}
		}while(flag_phone==false);
			System.out.println("Customer" + i + " registered sucessfully!");//2014 8.18 +1去掉了。�?  belle&didi
		}
	}

	/**
	 * 注册银行卡信�?
	 */
	public void registerAccount() { // 银行卡信息注�?有用户id注册银行卡信�?
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the customer's ID:-");
		long id;
		try{
		id = in.nextLong();
		
		long id1;
		int i;
		for (i = 0; i < Customer.getCount(); i++) {//getCount为已存在的customer的数量，用foreach遍历会报空指针异�?
			if (id == customer[i].getCustomer_id()) {
				int j = Account.getCount();
				account[j] = new Account();	
				boolean flag1;
			do{   
				flag1 = false;// 修复能创建相同accountid bug  20140818。�?   belle&didi
				System.out.print("Enter account's ID:-");
				id1 = in.nextLong();
				for(int k = 0;k <= j;k++)
				{
					if(account[k].getAccount_id()==id1){
						System.out.println("The accountId has been exsited!!Please Enter again!");
						flag1 =true;
						break;
					}
				}
			}while(flag1 == true);
			account[j].setAccount_id(id1);
				Date nowDate = new Date();
				account[j].setAccount_dateofopening(nowDate);				
				String ctype = null;
				boolean flag;
				in.nextLine();
				do {
					flag = true;
					System.out.println("Choice type : 1.Savings  2.Current");					
					System.out.print("Enter customer's Account_type:-");
					String choice = in.nextLine();
					if (choice.equals("1")) {
						ctype = "Savings";
					} else if (choice.equals("2")) {
						ctype = "Current";
					} else {
						System.out.println("Wrong chioce");
						flag = false;
					}
				} while (flag == false);
				account[j].setAccount_type(ctype);
				boolean flag_blalance;
				do{
					flag_blalance=false;
				System.out.print("Enter customer's Account_balance:-");
				try{
				double balance = in.nextDouble();
				account[j].setAccount_balance(balance);
				}catch(InputMismatchException e){
					System.out.println("Only for input digits,please enter again");
					flag_blalance = true;
					in.nextLine();
				}
				}while(flag_blalance==true);
				account[j].setCustomer_id(id);
				break;
			}
		}
		if (i == Customer.getCount()) {
			System.out.println("The Id is not exist!! please regiet customer first!");
		}
		}catch(InputMismatchException e){
			System.out.println("Only for input digits,please enter again");
			registerAccount();
		}
		
	}

	/**
	 * 查看用户信息
	 */
	public void viewCustomer() {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the customer's Id");
		long nod;
		try{
		nod = in.nextLong();
		boolean flag = false;
		for (int i = 0; i < Customer.getCount(); i++) {
			if (nod == customer[i].getCustomer_id()) {
				customer[i].show();
				flag = true;
			}
		}
		if (!flag) {
			System.out.println("The customer is not exist!");
		}
		}catch(InputMismatchException e){
			System.out.println("Only for inputing digits,please enter again");
			viewCustomer();
		}

	}

	/**
	 * 查看银行卡信�?
	 */
	public void viewAccount() { 
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the customer's Id");
		long nod;
		try{
		nod = in.nextLong();		
		boolean flag = false;
		int[] jarr = new int[10];
		int j = 1;
		for (int i = 0; i < Account.getCount(); i++) {
			if (nod == account[i].getCustomer_id()) {
				System.out.println(j + ".\t" + account[i].getAccount_id());
				jarr[j] = i;
				j++;
				flag = true;
			}
		}		
		if (!flag) {
			System.out.println("The account is not exist!");
		} else {
			
			boolean flag_check ;			
		do{
			flag_check = false;
			System.out.println("choose which count you want check ");
			int aa;
			try{
			aa = in.nextInt();
			if (aa < 1 || aa >= j) {
				System.out.println("input wrong number");
			} else {
				account[jarr[j]].show();
				flag_check = true;
			}
			}catch(InputMismatchException e){
				System.out.println("Only for inputing digits,please enter again...");
				flag_check = false;
			}
			}while(flag_check == false);
		}
		}catch(InputMismatchException e){
			System.out.println("Only for inputing digits,please enter again");
			viewAccount();
		}

	}

	/**
	 *  更新用户具体信息
	 */
	public void customerDetails() { 
		Scanner in = new Scanner(System.in);
		System.out.print("Enter the customer's ID that you want to update:-");
	try{
		long id = in.nextLong();
		in.nextLine();
		boolean flag = false;
		for (int i = 0; i < Customer.getCount(); i++) {
			if (customer[i].getCustomer_id() == id) {
				flag = true;
				String select;
				do {
					System.out.println("1.name:-"
							+ customer[i].getCustomer_name()
							+ "\t2.phonenumber:-"
							+ customer[i].getCustomer_phonenumber()
							+ "\t3.address:-" + customer[i].getCustomer_address()
							+ "\t4.exit");
					System.out.print("Enter your select:-");
					select = in.nextLine();
					switch (select) {
					case "1": {
						System.out.println("The old name:-"+customer[i].getCustomer_name());
						System.out.print("Enter the new name:-");
						customer[i].setCustomer_name(in.nextLine());
						break;
					}
					case "2": {
						System.out.println("The old phonenumber:-"+customer[i].getCustomer_phonenumber());
						System.out.print("Enter the new phonenumbr:-");
						customer[i].setCustomer_phonenumber(in.nextLine());
						break;
					}
					case "3": {
						System.out.println("The old address:-"+customer[i].getCustomer_address());
						System.out.print("Enter the new address:-");
						customer[i].setCustomer_address(in.nextLine());
						break;
					}
					case "4":
						new MainTest().AdminMenu();
					default: {
						System.out.println("Sorry,Enter wrong!Please enter again~");
						break;
					}
					}
				} while (!select.equals("4") );
			}
		}if(flag == false)
			System.out.println("The customer is not exit!!");    //修改用户不存在抛出异常bug   20140818 belle&didi
	}catch(InputMismatchException e){
		System.out.println("Only for inputing digits,please enter again");
		customerDetails();
	}
	}

	/**
	 * 更新银行卡具体信�?
	 */
	public void accountDetails() { 
		Scanner in = new Scanner(System.in);
		System.out.print("Enter the account's ID that you want to update:-");
	try{
		long id = in.nextLong();
		in.nextLine();
		int i;
		for (i = 0; i < Account.getCount(); i++) {
			if (account[i].getAccount_id() == id) {
				String select;
				do {
					System.out.println("1.type:-"
							+ account[i].getAccount_type()
							+ "\t2.dateofopening:-"
							+ account[i].getAccount_dateofopening() + "\t3.exit");
					System.out.print("Enter your select:-");
					select = in.nextLine();
					switch (select) {
					case "1": {
						System.out.println("The old type:-"+account[i].getAccount_type());
						String ctype = null;
						boolean flag;
						do {
							flag = true;
							System.out.println("Choice type : 1.Savings  2.Current");
							String choice = in.nextLine();
							if (choice.equals("1")) {
								ctype = "Savings";
							} else if (choice.equals("2")) {
								ctype = "Current";
							} else {
								System.out.println("Wrong chioce");
								flag = false;
							}
						} while (flag == false);
						
						account[i].setAccount_type(ctype);
						break;
					}
					case "2": {
						System.out.println("The old dateofopening:-"+account[i].getAccount_dateofopening());
						Date newDate = new Date();
						account[i].setAccount_dateofopening(newDate);
						System.out.println("The new dateofopening:-"+account[i].getAccount_dateofopening());
						break;
					}
					case "3": {
						break;
					}
					default: {
						System.out.println("Sorry,Enter wrong!Please enter again~");
						break;
					}
					}
				} while (!select.equals("3"));
				break;
			}
		}
		if (i == Account.getCount()) {
			System.out.println("The account is not exit!!");
		}
	}catch(InputMismatchException e){
		System.out.println("Only for inputing digits,please enter again");
		accountDetails();
	}
	}
}
